|
1
|
|
|
import {Promise} from 'bluebird' |
|
2
|
|
|
import path from 'path' |
|
3
|
|
|
|
|
4
|
|
|
import { |
|
5
|
|
|
cmsData, |
|
6
|
|
|
cmsEditor, |
|
7
|
|
|
abeEngine, |
|
8
|
|
|
cmsTemplates, |
|
9
|
|
|
abeExtend |
|
10
|
|
|
} from '../../cli' |
|
11
|
|
|
|
|
12
|
|
|
function add(obj, json, text, util) { |
|
13
|
|
|
var value = obj.value |
|
14
|
|
|
|
|
15
|
|
|
if(obj.key.indexOf('[') > -1) { |
|
16
|
|
|
var key = obj.key.split('[')[0] |
|
17
|
|
|
var index = obj.key.match(/[^\[]+?(?=\])/)[0] |
|
|
|
|
|
|
18
|
|
|
var prop = obj.key.replace(/[^\.]+?\./, '') |
|
19
|
|
|
key = getDataIdWithNoSlash(key) |
|
20
|
|
|
|
|
21
|
|
|
try { |
|
22
|
|
|
obj.value = eval('json[key][index].' + prop) |
|
|
|
|
|
|
23
|
|
|
} catch(e) { |
|
24
|
|
|
|
|
25
|
|
|
try { |
|
26
|
|
|
eval(`json[key][index].${prop} = ` + JSON.stringify(value)) |
|
|
|
|
|
|
27
|
|
|
}catch(e) { |
|
28
|
|
|
// no value found inside json OKEY |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
}else { |
|
32
|
|
|
try { |
|
33
|
|
|
obj.value = eval(`json.${getDataIdWithNoSlash(obj.key)}`) |
|
|
|
|
|
|
34
|
|
|
} catch(e) { |
|
35
|
|
|
// no value found inside json OKEY |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
obj.key = getDataIdWithNoSlash(obj.key) |
|
40
|
|
|
if (json != null && json.abe_meta != null) { |
|
41
|
|
|
obj.status = json.abe_meta.status |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
util.add(obj) |
|
45
|
|
|
|
|
46
|
|
|
return value |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function getDataIdWithNoSlash(key) { |
|
50
|
|
|
var trueKey = key |
|
51
|
|
|
if (trueKey.indexOf('/') > -1) { |
|
52
|
|
|
trueKey = trueKey.split('/') |
|
53
|
|
|
trueKey = trueKey[trueKey.length - 1] |
|
54
|
|
|
} |
|
55
|
|
|
return trueKey |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
function addToForm(match, text, json, util, arrayBlock, keyArray = null, i = 0) { |
|
59
|
|
|
var v = `{{${match}}}`, |
|
60
|
|
|
obj = cmsData.attributes.getAll(v, json) |
|
61
|
|
|
|
|
62
|
|
|
var realKey |
|
63
|
|
|
if(typeof keyArray !== 'undefined' && keyArray !== null) { |
|
64
|
|
|
realKey = obj.key.replace(/[^\.]+?\./, '') |
|
65
|
|
|
|
|
66
|
|
|
if(obj.key.indexOf(keyArray + '.') >= 0 && realKey.length > 0){ |
|
67
|
|
|
obj.keyArray = keyArray |
|
68
|
|
|
obj.realKey = realKey |
|
69
|
|
|
obj.key = keyArray + '[' + i + '].' + realKey |
|
70
|
|
|
obj.desc = obj.desc + ' ' + i, |
|
|
|
|
|
|
71
|
|
|
insertAbeEach(obj, text, json, util, arrayBlock) |
|
72
|
|
|
|
|
73
|
|
|
}else if(util.dontHaveKey(obj.key)) { |
|
74
|
|
|
obj.value = json[getDataIdWithNoSlash(obj.key)] |
|
75
|
|
|
json[getDataIdWithNoSlash(obj.key)] = add(obj, json, text, util) |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
}else if(util.dontHaveKey(obj.key) && cmsData.regex.isSingleAbe(v, text)) { |
|
79
|
|
|
realKey = obj.key//.replace(/\./g, '-') |
|
80
|
|
|
// obj.value = json[getDataIdWithNoSlash(realKey)] |
|
81
|
|
|
try { |
|
82
|
|
|
obj.value = eval(`json.${getDataIdWithNoSlash(realKey)}`) |
|
|
|
|
|
|
83
|
|
|
}catch(e) { |
|
84
|
|
|
obj.value = null |
|
85
|
|
|
} |
|
86
|
|
|
// json[getDataIdWithNoSlash(obj.key)] = |
|
87
|
|
|
add(obj, json, text, util) |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
function matchAttrAbe(text, json, util, arrayBlock) { |
|
92
|
|
|
var patt = /abe [^{{}}]+?(?=\}})/g, |
|
93
|
|
|
match |
|
94
|
|
|
// While regexp match HandlebarsJS template item => keepgoing |
|
95
|
|
|
while (match = patt.exec(text)) { |
|
96
|
|
|
addToForm(match[0], text, json, util, arrayBlock, null, null) |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function insertAbeEach (obj, text, json, util, arrayBlock) { |
|
101
|
|
|
if(typeof arrayBlock[obj.keyArray][obj.realKey] === 'undefined' || arrayBlock[obj.keyArray][obj.realKey] === null) { |
|
102
|
|
|
arrayBlock[obj.keyArray][obj.realKey] = [] |
|
103
|
|
|
} |
|
104
|
|
|
var exist = false |
|
105
|
|
|
Array.prototype.forEach.call(arrayBlock[obj.keyArray][obj.realKey], (block) => { |
|
106
|
|
|
if(block.key === obj.key) { |
|
107
|
|
|
exist = true |
|
108
|
|
|
} |
|
109
|
|
|
}) |
|
110
|
|
|
if(!exist) { |
|
111
|
|
|
arrayBlock[obj.keyArray][obj.realKey].push(obj) |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
function each(text, json, util, arrayBlock) { |
|
116
|
|
|
let pattEach = /(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g |
|
117
|
|
|
let patt = /{{(abe .*?["']) ?}}/g |
|
118
|
|
|
var textEach, match |
|
119
|
|
|
|
|
120
|
|
|
while (textEach = pattEach.exec(text)) { |
|
121
|
|
|
var i |
|
122
|
|
|
var keyArray = textEach[0].match(/#each (\n|.)*?\}/) |
|
123
|
|
|
keyArray = keyArray[0].slice(6, keyArray[0].length - 1) |
|
124
|
|
|
|
|
125
|
|
|
if(keyArray.split(' ').length > 1){ |
|
126
|
|
|
keyArray = keyArray.split(' ')[0] |
|
127
|
|
|
} |
|
128
|
|
|
arrayBlock[keyArray] = [] |
|
129
|
|
|
// ce while boucle sur les block de contenu {{abe}} |
|
130
|
|
|
while (match = patt.exec(textEach[0])) { |
|
131
|
|
|
var v = match[0] |
|
132
|
|
|
|
|
133
|
|
|
if(v.indexOf('abe') > -1){ |
|
134
|
|
|
if(json[keyArray]){ |
|
135
|
|
|
for (i = 0; i < json[keyArray].length; i++) { |
|
136
|
|
|
addToForm(v, text, json, util, arrayBlock, keyArray, i) |
|
137
|
|
|
} |
|
138
|
|
|
}else{ |
|
139
|
|
|
addToForm(v, text, json, util, arrayBlock, keyArray, 0) |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// ici on boucle a nouveau sur les champs pour les placer a la suite dans le formulaire |
|
145
|
|
|
var attrArray = [], |
|
146
|
|
|
length = 0 |
|
147
|
|
|
for(var index in arrayBlock[keyArray]) { |
|
|
|
|
|
|
148
|
|
|
attrArray.push(index) |
|
149
|
|
|
length = arrayBlock[keyArray][index].length |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
for (i = 0; i < length; i++) { |
|
153
|
|
|
for (var j = 0; j < attrArray.length; j++) { |
|
154
|
|
|
add(arrayBlock[keyArray][attrArray[j]][i], json, text, util) |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
function addSource(text, json, util) { |
|
161
|
|
|
// removing each blocks potentially containing abe data type |
|
162
|
|
|
let pattEach = /(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g |
|
163
|
|
|
text = text.replace(pattEach, '') |
|
164
|
|
|
|
|
165
|
|
|
var listReg = /({{abe.*type=[\'|\"]data.*}})/g |
|
166
|
|
|
var match |
|
167
|
|
|
|
|
168
|
|
|
while (match = listReg.exec(text)) { |
|
169
|
|
|
var obj = cmsData.attributes.getAll(match[0], json) |
|
170
|
|
|
|
|
171
|
|
|
if(obj.editable) { |
|
172
|
|
|
obj.value = json[getDataIdWithNoSlash(obj.key)] |
|
173
|
|
|
add(obj, json, text, util) |
|
174
|
|
|
}else { |
|
175
|
|
|
json[getDataIdWithNoSlash(obj.key)] = obj.source |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
function orderByTabindex(a, b) { |
|
181
|
|
|
if(a.order < b.order) { |
|
182
|
|
|
return -1 |
|
183
|
|
|
}else if(a.order > b.order) { |
|
184
|
|
|
return 1 |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return 0 |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
function orderBlock(util) { |
|
191
|
|
|
|
|
192
|
|
|
var formBlock = {} |
|
193
|
|
|
|
|
194
|
|
|
for(var tab in util.form) { |
|
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
var formBlockTab = {} |
|
197
|
|
|
for (var i = 0; i < util.form[tab].item.length; i++) { |
|
198
|
|
|
var blockName = (util.form[tab].item[i].block === '') ? 'default_' + i : util.form[tab].item[i].block |
|
199
|
|
|
if(util.form[tab].item[i].key.indexOf('[') > -1){ |
|
200
|
|
|
blockName = util.form[tab].item[i].key.split('[')[0] |
|
201
|
|
|
} |
|
202
|
|
|
if(typeof formBlockTab[blockName] === 'undefined' || formBlockTab[blockName] === null) { |
|
203
|
|
|
formBlockTab[blockName] = [] |
|
204
|
|
|
} |
|
205
|
|
|
formBlockTab[blockName].push(util.form[tab].item[i]) |
|
206
|
|
|
} |
|
207
|
|
|
if(typeof blockName !== 'undefined' && blockName !== null) { |
|
|
|
|
|
|
208
|
|
|
formBlockTab[blockName].sort(orderByTabindex) |
|
209
|
|
|
} |
|
210
|
|
|
if(typeof formBlock[tab] === 'undefined' || formBlock[tab] === null) { |
|
211
|
|
|
formBlock[tab] = {} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
var formBlockOrdered = {} |
|
215
|
|
|
var arKeys = Object.keys(formBlockTab).sort((a,b) => { |
|
216
|
|
|
if(parseFloat(formBlockTab[a][0].order) < parseFloat(formBlockTab[b][0].order)) { |
|
|
|
|
|
|
217
|
|
|
return -1 |
|
218
|
|
|
}else if(parseFloat(formBlockTab[a][0].order) > parseFloat(formBlockTab[b][0].order)) { |
|
219
|
|
|
return 1 |
|
220
|
|
|
} |
|
221
|
|
|
return 0 |
|
222
|
|
|
}) |
|
223
|
|
|
|
|
224
|
|
|
Array.prototype.forEach.call(arKeys, (arKey) => { |
|
225
|
|
|
formBlockOrdered[arKey] = formBlockTab[arKey] |
|
|
|
|
|
|
226
|
|
|
}) |
|
227
|
|
|
formBlock[tab] = formBlockOrdered |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
var formTabsOrdered = {} |
|
231
|
|
|
var arKeysTabs = Object.keys(formBlock).sort((a,b) => { |
|
232
|
|
|
if(parseFloat(formBlock[a][Object.keys(formBlock[a])[0]][0].order) < parseFloat(formBlock[b][Object.keys(formBlock[b])[0]][0].order)) { |
|
233
|
|
|
return -1 |
|
234
|
|
|
}else if(parseFloat(formBlock[a][Object.keys(formBlock[a])[0]][0].order) > parseFloat(formBlock[b][Object.keys(formBlock[b])[0]][0].order)) { |
|
235
|
|
|
return 1 |
|
236
|
|
|
} |
|
237
|
|
|
return 0 |
|
238
|
|
|
}) |
|
239
|
|
|
|
|
240
|
|
|
Array.prototype.forEach.call(arKeysTabs, (arKeysTab) => { |
|
241
|
|
|
if(arKeysTab !== 'slug') formTabsOrdered[arKeysTab] = formBlock[arKeysTab] |
|
|
|
|
|
|
242
|
|
|
}) |
|
243
|
|
|
|
|
244
|
|
|
formTabsOrdered['slug'] = formBlock['slug'] |
|
245
|
|
|
|
|
246
|
|
|
return formTabsOrdered |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
export function editor(text, json, documentLink, precontrib = false) { |
|
250
|
|
|
let p = new Promise((resolve) => { |
|
251
|
|
|
var util = new cmsEditor.form() |
|
252
|
|
|
var arrayBlock = [] |
|
253
|
|
|
|
|
254
|
|
|
cmsData.source.getDataList(path.dirname(documentLink), text, json) |
|
255
|
|
|
.then(() => { |
|
256
|
|
|
addSource(text, json, util) |
|
257
|
|
|
|
|
258
|
|
|
if (!precontrib) { |
|
259
|
|
|
text = cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist(text) |
|
260
|
|
|
text = cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist(text) |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
matchAttrAbe(text, json, util, arrayBlock) |
|
264
|
|
|
arrayBlock = [] |
|
265
|
|
|
each(text, json, util, arrayBlock) |
|
266
|
|
|
|
|
267
|
|
|
text = cmsData.source.removeDataList(text) |
|
268
|
|
|
if(typeof json.abe_meta !== 'undefined' && json.abe_meta !== null) { |
|
269
|
|
|
var links = json.abe_meta.link.split('/') |
|
270
|
|
|
var link = links.pop() |
|
271
|
|
|
json.abe_meta.cleanName = link.replace(/\..+$/, '') |
|
272
|
|
|
json.abe_meta.cleanFilename = links.join('/').replace(/\..+$/, '') |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
if (!precontrib) { |
|
276
|
|
|
// HOOKS beforeEditorFormBlocks |
|
277
|
|
|
json = abeExtend.hooks.instance.trigger('beforeEditorFormBlocks', json, text) |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
var blocks = orderBlock(util) |
|
281
|
|
|
|
|
282
|
|
|
if (!precontrib) { |
|
283
|
|
|
// HOOKS afterEditorFormBlocks |
|
284
|
|
|
blocks = abeExtend.hooks.instance.trigger('afterEditorFormBlocks', blocks, json, text) |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
abeEngine.instance.content = json |
|
288
|
|
|
resolve({ |
|
289
|
|
|
text: text, |
|
290
|
|
|
form: blocks, |
|
291
|
|
|
json: json |
|
292
|
|
|
}) |
|
293
|
|
|
}).catch(function(e) { |
|
294
|
|
|
console.error(e) |
|
295
|
|
|
}) |
|
296
|
|
|
}) |
|
297
|
|
|
|
|
298
|
|
|
return p |
|
299
|
|
|
} |